<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />

<meta name="viewport" content="width=device-width, initial-scale=1" />

<meta name="author" content="Jinlong Zhang" />

<meta name="date" content="2020-12-13" />

<title>The conversion between the WGS84 and the HK80 systems using HK80 and other tools</title>

<script src="data:application/javascript;base64,Ly8gUGFuZG9jIDIuOSBhZGRzIGF0dHJpYnV0ZXMgb24gYm90aCBoZWFkZXIgYW5kIGRpdi4gV2UgcmVtb3ZlIHRoZSBmb3JtZXIgKHRvCi8vIGJlIGNvbXBhdGlibGUgd2l0aCB0aGUgYmVoYXZpb3Igb2YgUGFuZG9jIDwgMi44KS4KZG9jdW1lbnQuYWRkRXZlbnRMaXN0ZW5lcignRE9NQ29udGVudExvYWRlZCcsIGZ1bmN0aW9uKGUpIHsKICB2YXIgaHMgPSBkb2N1bWVudC5xdWVyeVNlbGVjdG9yQWxsKCJkaXYuc2VjdGlvbltjbGFzcyo9J2xldmVsJ10gPiA6Zmlyc3QtY2hpbGQiKTsKICB2YXIgaSwgaCwgYTsKICBmb3IgKGkgPSAwOyBpIDwgaHMubGVuZ3RoOyBpKyspIHsKICAgIGggPSBoc1tpXTsKICAgIGlmICghL15oWzEtNl0kL2kudGVzdChoLnRhZ05hbWUpKSBjb250aW51ZTsgIC8vIGl0IHNob3VsZCBiZSBhIGhlYWRlciBoMS1oNgogICAgYSA9IGguYXR0cmlidXRlczsKICAgIHdoaWxlIChhLmxlbmd0aCA+IDApIGgucmVtb3ZlQXR0cmlidXRlKGFbMF0ubmFtZSk7CiAgfQp9KTsK"></script>

<style type="text/css">
  code{white-space: pre-wrap;}
  span.smallcaps{font-variant: small-caps;}
  span.underline{text-decoration: underline;}
  div.column{display: inline-block; vertical-align: top; width: 50%;}
  div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
  ul.task-list{list-style: none;}
    </style>


<style type="text/css">
  code {
    white-space: pre;
  }
  .sourceCode {
    overflow: visible;
  }
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */

</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
  var sheets = document.styleSheets;
  for (var i = 0; i < sheets.length; i++) {
    if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
    try { var rules = sheets[i].cssRules; } catch (e) { continue; }
    for (var j = 0; j < rules.length; j++) {
      var rule = rules[j];
      // check if there is a div.sourceCode rule
      if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue;
      var style = rule.style.cssText;
      // check if color or background-color is set
      if (rule.style.color === '' && rule.style.backgroundColor === '') continue;
      // replace div.sourceCode by a pre.sourceCode rule
      sheets[i].deleteRule(j);
      sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
    }
  }
})();
</script>




<link rel="stylesheet" href="data:text/css,body%20%7B%0Abackground%2Dcolor%3A%20%23fff%3B%0Amargin%3A%201em%20auto%3B%0Amax%2Dwidth%3A%20700px%3B%0Aoverflow%3A%20visible%3B%0Apadding%2Dleft%3A%202em%3B%0Apadding%2Dright%3A%202em%3B%0Afont%2Dfamily%3A%20%22Open%20Sans%22%2C%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0Afont%2Dsize%3A%2014px%3B%0Aline%2Dheight%3A%201%2E35%3B%0A%7D%0A%23TOC%20%7B%0Aclear%3A%20both%3B%0Amargin%3A%200%200%2010px%2010px%3B%0Apadding%3A%204px%3B%0Awidth%3A%20400px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Aborder%2Dradius%3A%205px%3B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Afont%2Dsize%3A%2013px%3B%0Aline%2Dheight%3A%201%2E3%3B%0A%7D%0A%23TOC%20%2Etoctitle%20%7B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%2015px%3B%0Amargin%2Dleft%3A%205px%3B%0A%7D%0A%23TOC%20ul%20%7B%0Apadding%2Dleft%3A%2040px%3B%0Amargin%2Dleft%3A%20%2D1%2E5em%3B%0Amargin%2Dtop%3A%205px%3B%0Amargin%2Dbottom%3A%205px%3B%0A%7D%0A%23TOC%20ul%20ul%20%7B%0Amargin%2Dleft%3A%20%2D2em%3B%0A%7D%0A%23TOC%20li%20%7B%0Aline%2Dheight%3A%2016px%3B%0A%7D%0Atable%20%7B%0Amargin%3A%201em%20auto%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20%23DDDDDD%3B%0Aborder%2Dstyle%3A%20outset%3B%0Aborder%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%20th%20%7B%0Aborder%2Dwidth%3A%202px%3B%0Apadding%3A%205px%3B%0Aborder%2Dstyle%3A%20inset%3B%0A%7D%0Atable%20td%20%7B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dstyle%3A%20inset%3B%0Aline%2Dheight%3A%2018px%3B%0Apadding%3A%205px%205px%3B%0A%7D%0Atable%2C%20table%20th%2C%20table%20td%20%7B%0Aborder%2Dleft%2Dstyle%3A%20none%3B%0Aborder%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%20thead%2C%20table%20tr%2Eeven%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Ap%20%7B%0Amargin%3A%200%2E5em%200%3B%0A%7D%0Ablockquote%20%7B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Apadding%3A%200%2E25em%200%2E75em%3B%0A%7D%0Ahr%20%7B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%3A%20none%3B%0Aborder%2Dtop%3A%201px%20solid%20%23777%3B%0Amargin%3A%2028px%200%3B%0A%7D%0Adl%20%7B%0Amargin%2Dleft%3A%200%3B%0A%7D%0Adl%20dd%20%7B%0Amargin%2Dbottom%3A%2013px%3B%0Amargin%2Dleft%3A%2013px%3B%0A%7D%0Adl%20dt%20%7B%0Afont%2Dweight%3A%20bold%3B%0A%7D%0Aul%20%7B%0Amargin%2Dtop%3A%200%3B%0A%7D%0Aul%20li%20%7B%0Alist%2Dstyle%3A%20circle%20outside%3B%0A%7D%0Aul%20ul%20%7B%0Amargin%2Dbottom%3A%200%3B%0A%7D%0Apre%2C%20code%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0Aborder%2Dradius%3A%203px%3B%0Acolor%3A%20%23333%3B%0Awhite%2Dspace%3A%20pre%2Dwrap%3B%20%0A%7D%0Apre%20%7B%0Aborder%2Dradius%3A%203px%3B%0Amargin%3A%205px%200px%2010px%200px%3B%0Apadding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Acode%20%7B%0Afont%2Dfamily%3A%20Consolas%2C%20Monaco%2C%20%27Courier%20New%27%2C%20monospace%3B%0Afont%2Dsize%3A%2085%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0Apadding%3A%202px%200px%3B%0A%7D%0Adiv%2Efigure%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0Aimg%20%7B%0Abackground%2Dcolor%3A%20%23FFFFFF%3B%0Apadding%3A%202px%3B%0Aborder%3A%201px%20solid%20%23DDDDDD%3B%0Aborder%2Dradius%3A%203px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Amargin%3A%200%205px%3B%0A%7D%0Ah1%20%7B%0Amargin%2Dtop%3A%200%3B%0Afont%2Dsize%3A%2035px%3B%0Aline%2Dheight%3A%2040px%3B%0A%7D%0Ah2%20%7B%0Aborder%2Dbottom%3A%204px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Apadding%2Dbottom%3A%202px%3B%0Afont%2Dsize%3A%20145%25%3B%0A%7D%0Ah3%20%7B%0Aborder%2Dbottom%3A%202px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Ah4%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23f7f7f7%3B%0Amargin%2Dleft%3A%208px%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Ah5%2C%20h6%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23ccc%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Aa%20%7B%0Acolor%3A%20%230033dd%3B%0Atext%2Ddecoration%3A%20none%3B%0A%7D%0Aa%3Ahover%20%7B%0Acolor%3A%20%236666ff%3B%20%7D%0Aa%3Avisited%20%7B%0Acolor%3A%20%23800080%3B%20%7D%0Aa%3Avisited%3Ahover%20%7B%0Acolor%3A%20%23BB00BB%3B%20%7D%0Aa%5Bhref%5E%3D%22http%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0Aa%5Bhref%5E%3D%22https%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0A%0Acode%20%3E%20span%2Ekw%20%7B%20color%3A%20%23555%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Edt%20%7B%20color%3A%20%23902000%3B%20%7D%20%0Acode%20%3E%20span%2Edv%20%7B%20color%3A%20%2340a070%3B%20%7D%20%0Acode%20%3E%20span%2Ebn%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Efl%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Ech%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Est%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Eco%20%7B%20color%3A%20%23888888%3B%20font%2Dstyle%3A%20italic%3B%20%7D%20%0Acode%20%3E%20span%2Eot%20%7B%20color%3A%20%23007020%3B%20%7D%20%0Acode%20%3E%20span%2Eal%20%7B%20color%3A%20%23ff0000%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Efu%20%7B%20color%3A%20%23900%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Eer%20%7B%20color%3A%20%23a61717%3B%20background%2Dcolor%3A%20%23e3d2d2%3B%20%7D%20%0A" type="text/css" />




</head>

<body>




<h1 class="title toc-ignore">The conversion between the WGS84 and the HK80 systems using HK80 and other tools</h1>
<h4 class="author">Jinlong Zhang</h4>
<h4 class="date">12/13/2020</h4>



<p>HK80 (Hong Kong 1980, EPSG: 2326) is one of the most widely used geographical coordinate systems in Hong Kong. Compared to latitude and longitude in the WGS84 (EPSG: 4326) system, HK80 uses X and Y for denoting easting and northing from the origin. This tutorial describes how to convert HK80 coordinates to WGS84 coordinate system using the HK80 package.</p>
<p>The HK80 R package was develped based on the equations provided by the Survey &amp; Mapping Office, Lands Department, Hong Kong SAR Governemt, which is available at (<a href="https://www.geodetic.gov.hk/common/data/pdf/explanatorynotes_c.pdf" class="uri">https://www.geodetic.gov.hk/common/data/pdf/explanatorynotes_c.pdf</a>).</p>
<p>Over the past few years, a number of new tools have appreared. For example, the HK80 online conversion tool of the Survey and Mapping Office, Lands Department (<a href="https://www.geodetic.gov.hk/en/services/tform/tform.aspx" class="uri">https://www.geodetic.gov.hk/en/services/tform/tform.aspx</a>) provides an API that can process user’s query. The <code>sf</code> R package provides the function <code>st_transform</code> based on the <code>proj</code> package, making the conversion between different coordinate systems very convenient. There is also a Python package called <code>hk80</code> (<a href="https://pypi.org/project/hk80/" class="uri">https://pypi.org/project/hk80/</a>) for converting the coordinates, basing upon the more widely used package <code>pyproj</code> (<a href="https://pyproj4.github.io/pyproj/stable/#" class="uri">https://pyproj4.github.io/pyproj/stable/#</a>, <a href="https://proj.org/" class="uri">https://proj.org/</a>).</p>
<p>The online conversion tool supplied by the Hong Kong Lands Department is the first choice among different conversion tools. However, because the server may have restrictions when querying a large amount of data, the conversion tool may not be very convinient (although I have not tried to get my IP blocked).</p>
<p><code>sf</code> and <code>HK80</code>, on the other hand, do not have these restrictions. Both the results from <code>sf</code> and <code>HK80</code> are reliable, but <code>sf</code> is easier to use. The results of the HK80 package can be used as a reference.</p>
<p>The conversion between DMS to decimal format and vice versa are also provided as an appendix.</p>
<div id="hk80grid-to-wgs84" class="section level1">
<h1>HK80GRID to WGS84</h1>
<div id="hk80-r-package" class="section level2">
<h2>HK80 R package</h2>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(HK80)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">HK1980GRID_TO_WGS84GEO</span>(<span class="at">N =</span> <span class="fl">820359.389</span>, <span class="at">E =</span> <span class="fl">832591.320</span>)</span></code></pre></div>
<pre><code>##   latitude longitude
## 1 22.32225  114.1412</code></pre>
</div>
<div id="the-official-online-conversion-tool" class="section level2">
<h2>The official online conversion tool</h2>
<p>the Geodetic Survey Section, Lands Department, Hong Kong SAR Gov.</p>
<p>API example: <code>http://www.geodetic.gov.hk/transform/v2/?inSys=hkgrid&amp;e=832591.320&amp;n=820359.389</code></p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(jsonlite)</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>data1 <span class="ot">&lt;-</span> <span class="fu">fromJSON</span>(<span class="st">&quot;http://www.geodetic.gov.hk/transform/v2/?inSys=hkgrid&amp;e=832591.320&amp;n=820359.389&quot;</span>)</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(data1)</span></code></pre></div>
<pre><code>##  [1] &quot;wgsLat&quot;      &quot;wgsLong&quot;     &quot;hkLat&quot;       &quot;hkLong&quot;      &quot;utmGridZone&quot;
##  [6] &quot;utmGridE&quot;    &quot;utmGridN&quot;    &quot;utmRefZone&quot;  &quot;utmRefE&quot;     &quot;utmRefN&quot;</code></pre>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>data1<span class="sc">$</span>wgsLat</span></code></pre></div>
<pre><code>## [1] 22.32224</code></pre>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>data1<span class="sc">$</span>wgsLong</span></code></pre></div>
<pre><code>## [1] 114.1412</code></pre>
</div>
<div id="the-sf-package" class="section level2">
<h2>the sf package</h2>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sf)</span></code></pre></div>
<pre><code>## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1</code></pre>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot">=</span> <span class="fu">st_point</span>(<span class="fu">c</span>(<span class="fl">832591.320</span>, <span class="fl">820359.389</span>))</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>sfc <span class="ot">=</span> <span class="fu">st_sfc</span>(p1, <span class="at">crs =</span> <span class="dv">2326</span>)</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>(<span class="fu">st_transform</span>(sfc, <span class="dv">4326</span>))</span></code></pre></div>
<pre><code>## Geometry set for 1 feature 
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 114.1412 ymin: 22.32224 xmax: 114.1412 ymax: 22.32224
## Geodetic CRS:  WGS 84</code></pre>
<pre><code>## POINT (114.1412 22.32224)</code></pre>
</div>
</div>
<div id="wgs84-to-hk80grid" class="section level1">
<h1>WGS84 to HK80GRID</h1>
<div id="the-hk80-r-package" class="section level2">
<h2>the HK80 R package</h2>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(HK80)</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="fu">WGS84GEO_TO_HK1980GRID</span>(<span class="at">latitude =</span> <span class="fl">22.32224</span>, <span class="at">longitude =</span> <span class="fl">114.14118</span>)</span></code></pre></div>
<pre><code>##          N        E
## 1 820358.7 832591.4</code></pre>
</div>
<div id="the-official-online-conversion-tool-1" class="section level2">
<h2>The official online conversion tool</h2>
<p>from the Geodetic Survey Section, Lands Department, Hong Kong SAR Gov.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Copy the following URL to browser</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="co"># http://www.geodetic.gov.hk/transform/v2/?inSys=wgsgeog&amp;outSys=hkgrid&amp;lat=22.32224&amp;long=114.14118&amp;h=23.128</span></span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="co"># {&quot;hkN&quot;: 820358.910,&quot;hkE&quot;: 832590.508,&quot;hkpd&quot;: 26.009}</span></span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(jsonlite)</span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a>data1 <span class="ot">&lt;-</span> <span class="fu">fromJSON</span>(<span class="st">&quot;http://www.geodetic.gov.hk/transform/v2/?inSys=wgsgeog&amp;outSys=hkgrid&amp;lat=22.32224&amp;long=114.14118&amp;h=23.128&quot;</span>)</span>
<span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-8"><a href="#cb16-8" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(data1)</span></code></pre></div>
<pre><code>## [1] &quot;hkN&quot;  &quot;hkE&quot;  &quot;hkpd&quot;</code></pre>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>data1<span class="sc">$</span>hkN</span></code></pre></div>
<pre><code>## [1] 820358.9</code></pre>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>data1<span class="sc">$</span>hkE</span></code></pre></div>
<pre><code>## [1] 832590.5</code></pre>
</div>
<div id="the-sf-package-1" class="section level2">
<h2>the sf package</h2>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sf)</span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot">=</span> <span class="fu">st_point</span>(<span class="fu">c</span>(<span class="fl">114.14118</span>, <span class="fl">22.32224</span>))</span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a>sfc <span class="ot">=</span> <span class="fu">st_sfc</span>(p1, <span class="at">crs =</span> <span class="dv">4326</span>)</span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a>(ccc <span class="ot">&lt;-</span> <span class="fu">st_transform</span>(sfc, <span class="dv">2326</span>))</span></code></pre></div>
<pre><code>## Geometry set for 1 feature 
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 832590.5 ymin: 820358.9 xmax: 832590.5 ymax: 820358.9
## Projected CRS: Hong Kong 1980 Grid System</code></pre>
<pre><code>## POINT (832590.5 820358.9)</code></pre>
</div>
</div>
<div id="appendix-dms-to-demical-and-vice-versa" class="section level1">
<h1>Appendix: DMS to demical and vice versa</h1>
<div id="using-the-sp-package" class="section level2">
<h2>Using the sp package</h2>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sp)</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="fu">dd2dms</span>(<span class="fl">114.14118</span>) <span class="co"># decimal to Degree, Minute, Second format</span></span></code></pre></div>
<pre><code>## [1] 114d8&#39;28.248&quot;E</code></pre>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="fu">dd2dms</span>(<span class="fl">114.14118</span>)) <span class="co">#</span></span></code></pre></div>
<pre><code>## [1] 114.1412</code></pre>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">char2dms</span>(<span class="st">&quot;47d15&#39;6.12</span><span class="sc">\&quot;</span><span class="st">E&quot;</span>)</span></code></pre></div>
<pre><code>## [1] 47d15&#39;6.12&quot;E</code></pre>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="fu">char2dms</span>(<span class="st">&quot;47d15&#39;6.12</span><span class="sc">\&quot;</span><span class="st">E&quot;</span>))</span></code></pre></div>
<pre><code>## [1] 47.2517</code></pre>
</div>
<div id="using-the-biogeo-package" class="section level2">
<h2>Using the biogeo package</h2>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(biogeo)</span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a>res <span class="ot">&lt;-</span> <span class="fu">dms2dd</span>(<span class="dv">47</span>,<span class="dv">15</span>,<span class="fl">6.12</span>,<span class="st">&quot;E&quot;</span>) <span class="co"># ns letters (N,S,E,W)</span></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(res)</span></code></pre></div>
<pre><code>## [1] 47.2517</code></pre>
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="fu">dd2dmslong</span>(<span class="fl">114.14118</span>)</span></code></pre></div>
<pre><code>##   xdeg xmin xsec EW
## 1  114    8 28.2  E</code></pre>
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="fu">dd2dmslat</span>(<span class="fl">22.32224</span>)</span></code></pre></div>
<pre><code>##   ydeg ymin ysec NS
## 1   22   19 20.1  N</code></pre>
</div>
</div>
<div id="sessioninfo" class="section level1">
<h1>SessionInfo</h1>
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sessionInfo</span>()</span></code></pre></div>
<pre><code>## R version 4.1.1 (2021-08-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19041)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=C                          
## [2] LC_CTYPE=English_Hong Kong SAR.1252   
## [3] LC_MONETARY=English_Hong Kong SAR.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_Hong Kong SAR.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] biogeo_1.0     sp_1.4-5       sf_1.0-2       jsonlite_1.7.2 HK80_0.0.3    
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.7         lattice_0.20-44    prettyunits_1.1.1  class_7.3-19      
##  [5] ps_1.6.0           assertthat_0.2.1   rprojroot_2.0.2    digest_0.6.27     
##  [9] utf8_1.2.2         R6_2.5.0           evaluate_0.14      e1071_1.7-8       
## [13] pillar_1.6.2       rlang_0.4.11       curl_4.3.2         rstudioapi_0.13   
## [17] raster_3.4-13      vegan_2.5-7        callr_3.7.0        jquerylib_0.1.4   
## [21] Matrix_1.3-4       rmarkdown_2.10     splines_4.1.1      desc_1.3.0        
## [25] devtools_2.4.2     foreign_0.8-81     stringr_1.4.0      proxy_0.4-26      
## [29] compiler_4.1.1     xfun_0.25          pkgconfig_2.0.3    pkgbuild_1.2.0    
## [33] mgcv_1.8-36        htmltools_0.5.1.1  tidyselect_1.1.1   tibble_3.1.3      
## [37] roxygen2_7.1.1     codetools_0.2-18   permute_0.9-5      fansi_0.5.0       
## [41] crayon_1.4.1       dplyr_1.0.7        withr_2.4.2        MASS_7.3-54       
## [45] grid_4.1.1         nlme_3.1-152       lifecycle_1.0.0    DBI_1.1.1         
## [49] magrittr_2.0.1     units_0.7-2        KernSmooth_2.23-20 cli_3.0.1         
## [53] stringi_1.7.3      cachem_1.0.5       fs_1.5.0           remotes_2.4.0     
## [57] testthat_3.0.4     xml2_1.3.2         bslib_0.2.5.1      ellipsis_0.3.2    
## [61] vctrs_0.3.8        generics_0.1.0     tools_4.1.1        glue_1.4.2        
## [65] purrr_0.3.4        parallel_4.1.1     processx_3.5.2     pkgload_1.2.1     
## [69] fastmap_1.1.0      yaml_2.2.1         cluster_2.1.2      maptools_1.1-1    
## [73] sessioninfo_1.1.1  classInt_0.4-3     memoise_2.0.0      knitr_1.33        
## [77] sass_0.4.0         usethis_2.0.1</code></pre>
</div>
<div id="further-reading" class="section level1">
<h1>Further Reading</h1>
<ul>
<li><p>Jeroen Ooms (2014). The jsonlite Package: A Practical and Consistent Mapping Between JSON Data and R Objects. arXiv:1403.2805 URL <a href="https://arxiv.org/abs/1403.2805" class="uri">https://arxiv.org/abs/1403.2805</a>.</p></li>
<li><p>Jinlong Zhang (2016). HK80: Conversion Tools for HK80 Geographical Coordinate System. R package version 0.0.2. <a href="https://CRAN.R-project.org/package=HK80" class="uri">https://CRAN.R-project.org/package=HK80</a></p></li>
<li><p>Mark Robertson (2016). biogeo: Point Data Quality Assessment and Coordinate Conversion. R package version 1.0. <a href="https://CRAN.R-project.org/package=biogeo" class="uri">https://CRAN.R-project.org/package=biogeo</a></p></li>
<li><p>Pebesma, E., 2018. Simple Features for R: Standardized Support for Spatial Vector Data. The R Journal 10 (1), 439-446, <a href="https://doi.org/10.32614/RJ-2018-009" class="uri">https://doi.org/10.32614/RJ-2018-009</a></p></li>
<li><p>Roger S. Bivand, Edzer Pebesma, Virgilio Gomez-Rubio,</p>
<ol style="list-style-type: decimal">
<li>Applied spatial data analysis with R, Second edition. Springer, NY. <a href="https://asdar-book.org/" class="uri">https://asdar-book.org/</a></li>
</ol></li>
<li><p><a href="https://pypi.org/project/hk80/" class="uri">https://pypi.org/project/hk80/</a></p></li>
<li><p><a href="https://spatialreference.org/ref/?search=Hong+Kong" class="uri">https://spatialreference.org/ref/?search=Hong+Kong</a></p></li>
<li><p><a href="https://www.geodetic.gov.hk/en/download.htm" class="uri">https://www.geodetic.gov.hk/en/download.htm</a></p></li>
</ul>
</div>



<!-- code folding -->


<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>
